package monitor

import (
	consts2 "MoSkeleton/config/consts"
	"MoSkeleton/framework"
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	"MoSkeleton/services"
	"MoSkeleton/web/pogos/request/monitor"
	"MoSkeleton/web/v1/http/base"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strings"
	"time"
)

type SysLoginInfoCtrl struct {
	base.BaseCtrl
}

func (s *SysLoginInfoCtrl) InitCtrl() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			c.Next()
		}()
		var operLog4GinReq *monitor.OperLog4GinReq
		utils.GinGet(c, consts.GinContext_OperLog_key, &operLog4GinReq)
		if operLog4GinReq == nil {
			framework.MoLogger.Error("未找到操作日志信息")
			return
		}
		operLog4GinReq.Title = "登录日志管理"
	}
}

// GetLoginInfoPage 获取登录日志列表
// @Tags 登录日志管理
// @Summary 获取登录日志列表
//
//	@Security ApiKeyAuth
//
// @Param data query monitor.SysLoginInforPageReq true "分页登录日志参数"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "返回登录日志列表"
// @Router /monitor/logininfor/list [get]
func (s *SysLoginInfoCtrl) GetLoginInfoPage(c *gin.Context) {
	loginInfoReq := monitor.SysLoginInforPageReq{}
	if err := c.ShouldBindQuery(&loginInfoReq); err != nil {
		framework.MoLogger.Error("获取登录日志列表失败", zap.Error(err))
		response.FailMsg(c, consts.CommSelectFailCode, "获取登录日志列表失败")
		return
	}
	loginInfoReq.ShouldBindParams(c)
	loginInfoPage, err := services.ServiceGroupInst.SystemGroupInst.SysLoginInfoServiceInst.GetLoginInfoPage(&loginInfoReq)
	if err != nil {
		framework.MoLogger.Error("获取登录日志列表失败", zap.Error(err))
		response.FailMsg(c, consts.CommSelectFailCode, "获取登录日志列表失败")
		return
	}
	response.Success(c, "获取登录日志列表成功", loginInfoPage)
}

// DeleteLoginInfos 删除登录日志
// @Tags 登录日志管理
// @Summary 删除登录日志
//
//	@Security ApiKeyAuth
//
// @Param ids path string true "登录日志ID"
// @Success 200 {object} response.Response{msg=string} "删除登录日志"
// @Router /monitor/logininfor/{ids} [delete]
func (s *SysLoginInfoCtrl) DeleteLoginInfos(c *gin.Context) {
	loginInfoIdsStr := c.Param("ids")
	if loginInfoIdsStr == "" {
		framework.MoLogger.Error("参数错误")
		response.FailMsg(c, consts.ValidatorParamsCheckFailCode, "参数错误")
		return
	}
	idStrs := strings.Split(loginInfoIdsStr, ",")
	ids := utils.StrsToUint64s(&idStrs)
	ok, err := services.ServiceGroupInst.SystemGroupInst.SysLoginInfoServiceInst.DeleteLoginInfos(ids)
	if err != nil {
		framework.MoLogger.Error("删除登录日志失败", zap.Error(err))
		response.FailMsg(c, consts.CommDeleteFailCode, "删除登录日志失败")
		return
	}
	if !ok {
		framework.MoLogger.Error("删除登录日志失败")
		response.FailMsg(c, consts.CommDeleteFailCode, "删除登录日志失败")
		return
	}
	response.Success(c, "删除登录日志成功", nil)
}

// CleanTable 清空登录日志
// @Tags 登录日志管理
// @Summary 清空登录日志
//
//	@Security ApiKeyAuth
//
// @Success 200 {object} response.Response{msg=string} "清空登录日志"
// @Router /monitor/logininfor/clean [delete]
func (s *SysLoginInfoCtrl) CleanTable(c *gin.Context) {
	err := services.ServiceGroupInst.SystemGroupInst.SysLoginInfoServiceInst.CleanTable()
	if err != nil {
		framework.MoLogger.Error("清空登录日志失败", zap.Error(err))
		response.FailMsg(c, consts.CommDeleteFailCode, "清空登录日志失败")
		return
	}
	response.Success(c, "清空登录日志成功", nil)
}

// ExportLoginInfos 导出登录日志
// @Tags 登录日志管理
// @Summary 导出登录日志
// @Security ApiKeyAuth
// @Param data query monitor.SysLoginInforPageReq true "分页登录日志参数"
// @Success 200 {object} response.Response{msg=string} "导出登录日志"
// @Router /monitor/logininfor/export [POST]
func (s *SysLoginInfoCtrl) ExportLoginInfos(c *gin.Context) {
	s.IgnoreLogResponseData(c)
	req := monitor.SysLoginInforPageReq{}
	if err := c.ShouldBindQuery(&req); err != nil {
		framework.MoLogger.Error("导出登录日志失败", zap.Error(err))
		response.FailMsg(c, consts.ValidatorParamsCheckFailCode, "导出登录日志失败")
		return
	}
	buf, err := services.ServiceGroupInst.SystemGroupInst.SysLoginInfoServiceInst.ExportLoginInfos(&req)
	if err != nil {
		framework.MoLogger.Error("导出登录日志失败", zap.Error(err))
		response.FailMsg(c, consts.CommSelectFailCode, "导出登录日志失败")
		return
	}
	response.SuccessWithFileData(c, fmt.Sprintf(consts2.Excel_Export_LoginInfo, time.Now().Unix()), buf)
}
